﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;

public class TestCard : MonoBehaviour {

	private List<int> list = new List<int> ();
	//private Cards card = new Cards ();
	private int MaxCard = 13;
	// Use this for initialization

	public GameObject startButton;
	private bool Tso = false;

	void Start(){
		startButton.GetComponent<Button>().onClick.AddListener(delegate(){
			StartIt();
		});
	}

	private void ChangeStart(GameObject go = null){
		GameObject.Find ("Log").GetComponent<Text> ().text = "";
		int i = 0;
		while (i < 25) {
			i++;
			StartIt ();
		}
	}

	private void StartIt(){
		list = new List<int> ();
		/*
		list.Add (1);
		list.Add(2);
		list.Add(3);
		list.Add(4);
		list.Add(5);

		list.Add(16);
		list.Add(17);
		list.Add(18);
		list.Add(19);
		list.Add(20);

		list.Add(49);
		list.Add(50);
		list.Add(51);

*/
		while( list.Count < 13 ){

			int take = Random.Range(1,53);

			if (!list.Contains (take)) {
				list.Add (take);
			}
		}

		CheckCombination ();


	}

	public void CheckCombination(){
		GameObject.Find ("Log").GetComponent<Text> ().text = "";
		result = new List<List<Combination>> ();
		string c = "";
		list.Sort ();
		foreach(int b in list){
			c += ChangName(b) +" ";
		}
		GameObject.Find ("Log").GetComponent<Text> ().text += "发牌:"+c +"\n";


		List<List<int>> ThreeStraightAndSameColor = TryThreeStraightAndSameColor (list);
		if ( ThreeStraightAndSameColor != null ) {
			for(int i = 0 ; i < ThreeStraightAndSameColor.Count ; i++ ){
				foreach(int b in ThreeStraightAndSameColor[i]){
					c += ChangName(b) +" ";
				}
				Debug.Log (c);
			}
			Debug.Log ("三同花顺\n");
			return;
		}
		List<List<int>> ThreeSameColor = TryThreeSameColor (list);
		if ( ThreeSameColor != null ) {
			for(int i = 0 ; i < ThreeSameColor.Count ; i++ ){
				foreach(int b in ThreeSameColor[i]){
					c += ChangName(b) +" ";
				}
				//Debug.Log (c);
			}
			Debug.Log ("三同花\n");
			return;
		}
		List<List<int>> ThreeStraight = TryThreeStraight (list);
		if ( ThreeStraight != null ) {
			for(int i = 0 ; i < ThreeStraight.Count ; i++ ){
				foreach(int b in ThreeStraight[i]){
					c += ChangName(b) +" ";
				}
				Debug.Log (c);
			}
			Debug.Log ("三顺子\n");
			return;
		}

		List<int> SixAndHalf = TrySixAndHalf (list);

		if ( TrySixAndHalf (list) != null ) {

			foreach(int b in SixAndHalf){
				c += ChangName(b) +" ";
			}

			Debug.Log (c);
			Debug.Log ("出现了六对半\n");
			return;
		}

		GroudCard ();
	}



	List<List<Combination>> result;
	private void GroudCard(){

		List<int> headType = new List<int>();
		List<int> bodyType = new List<int>();


		int count = 0;
		for(int i = 0 ; i < 10 ; i++){


			count++;
			List<int> l = new List<int> ( list.ToArray () );
			List<Combination> conbin = new List<Combination> ();
			Combination combincell2 = tryIt (headType,conbin,l);

			if (combincell2 == null || combincell2.Type == 0) {
				continue;
			}

			if ( count > 1) {
				if( headType.Count > 0 ){
					headType.RemoveAt(0);
				}
			}
			Combination combincell;
			tryIt (bodyType,conbin,l);



			combincell = GetSub (l);
			if (combincell != null) {
				combincell.getValue ().Add (l [0]);
				conbin.Add (combincell);

			} else {
				combincell = new Combination ();
				combincell.Type = Combination.Chaos;
				combincell.setValue (l);
				conbin.Add (combincell);
			}



			result.Add (conbin);
		}


		for( int s = 0 ; s < result.Count ; s++ ){

			GameObject.Find ("Log").GetComponent<Text> ().text += "组合:" + s+"\n";
			string c = "";
			GameObject.Find ("Log").GetComponent<Text> ().text += "组合尾蹲 "+ s +" 牌型:"+result [s] [0].Type+"\n";
			foreach(int b in result [s] [0].getValue()){
				c += ChangName(b) +" ";
			}

			GameObject.Find ("Log").GetComponent<Text> ().text += "组合尾蹲: " + c+"\n";
			GameObject.Find ("Log").GetComponent<Text> ().text += "组合中蹲 " + s + " 牌型:" + result [s] [1].Type+"\n";

			c = "";
			foreach(int b in result [s] [1].getValue()){
				c += ChangName(b) +" ";
			}

			GameObject.Find ("Log").GetComponent<Text> ().text += "组合中蹲:" + c+"\n";
			GameObject.Find ("Log").GetComponent<Text> ().text += "组合头蹲 "+ s +" 牌型:"+result [s] [2].Type+"\n";

			c = "";

			if(result [s] [2].getValue().Count < 3){
				Debug.Log ("--------------");
			}

			foreach(int b in result [s] [2].getValue()){
				c += ChangName(b) +" ";
			}
			GameObject.Find ("Log").GetComponent<Text> ().text += "组合头蹲:" + c+"\n";
			Debug.Log ("\n");
		}

	}

	private Combination tryIt(List<int> Types,List<Combination> conbin,List<int> l){

		Combination combincell;
		if (Types.Contains (Combination.StraightColor) && l.Count > 3) {
			
			combincell = GetStraghtColor (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.Branch) && l.Count > 3) {

			combincell = GetBranch (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);

				int random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.RemoveAt (random);
				return combincell;
			}

		}

		if (!Types.Contains (Combination.Gourd) && l.Count > 3 ) {

			combincell = GetGourds (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.SameColor) && l.Count > 3 ) {

			combincell = GetSameColors (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.Straight) && l.Count > 3 ) {

			combincell = GetStraight (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.Three) && l.Count > 3 ) {

			combincell = GetThrees (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);

				int random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.RemoveAt (random);

				random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.RemoveAt (random);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.DoubleSub) && l.Count > 3) {

			combincell = GetDoubleSub (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);

				int random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.RemoveAt (random);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.Sub) && l.Count > 3) {

			combincell = GetSub (l);
			if (combincell != null) {
				conbin.Add (combincell);
				Types.Add (combincell.Type);

				int random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.RemoveAt (random);

				random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.RemoveAt (random);

				random = Random.Range (0, l.Count-1);
				combincell.getValue ().Add (l [random]);
				l.RemoveAt (random);
				return combincell;
			}
		}

		if (!Types.Contains (Combination.Chaos) && l.Count > 3) {

			combincell = new Combination ();
			combincell.Type = Combination.Chaos;
			List<int> newlist = new List<int> ();
			for( int i = 0 ; i < 5 ; i++ ){
				newlist.Add (l [i]);
			}
			combincell.setValue (newlist);
			conbin.Add (combincell);
			for( int i = 0 ; i < newlist.Count ; i++ ){
				l.Remove(newlist[i]);
			}
			return combincell;
		}

		return null;

	}

	//检查是否可以组成同花顺
	private List<int> TryStraightColor(List<int> value){

		value.Sort ();

		List<int> newList = CheckStraght (value,5,true);

		if( newList != null ){
			if (CheckColor (newList)) {
				return newList;
			} else {
				return null;
			}

		} else {
			return null;
		}

		return null;

	}

	//检查是否可以组成六对半
	private List<int> TrySixAndHalf(List<int> value){
		List<int> backUp = new List<int>(value.ToArray());
		value.Sort ();

		List<int> newList =  GetIronOrThreeOrDouble (value,2,true);
		List<int> newList2 = GetIronOrThreeOrDouble (value,2,true);
		List<int> newList3 = GetIronOrThreeOrDouble (value,2,true);
		List<int> newList4 = GetIronOrThreeOrDouble (value,2,true);
		List<int> newList5 = GetIronOrThreeOrDouble (value,2,true);
		List<int> newList6 = GetIronOrThreeOrDouble (value,2,true);

		if (newList != null && newList2 != null && newList3 != null && newList4 != null && newList5 != null && newList6 != null) {
			List<int> temp = new List<int> ();
			temp.AddRange (newList.ToArray ());
			temp.AddRange (newList2.ToArray ());
			temp.AddRange (newList3.ToArray ());
			temp.AddRange (newList4.ToArray ());
			temp.AddRange (newList5.ToArray ());
			temp.AddRange (newList6.ToArray ());
			return temp;
		} 

		list = backUp;
		return null;
	}

	//检查是否可以组成三顺子
	private List<List<int>> TryThreeStraight(List<int> value){
		List<int> backUp = new List<int>(value.ToArray());
		value.Sort ();

		List<int> newList = CheckStraght (value,5,true);
		List<int> newList2 = CheckStraght (value,5,true);
		List<int> newList3 = CheckStraght (value,3,true);

		if (newList != null && newList2 != null && newList3 != null) {

			List<List<int>> temp = new List<List<int>> ();
			temp.Add (newList);
			temp.Add (newList2);
			temp.Add (newList3);
			return temp;

		}
		list = backUp;
		return null;
	}

	//检查是否可以组成三同花顺
	private List<List<int>> TryThreeStraightAndSameColor(List<int> value){
		List<int> backUp = new List<int>(value.ToArray());
		value.Sort ();

		List<int> newList = CheckStraght (value,5,true);
		List<int> newList2 = CheckStraght (value,5,true);
		List<int> newList3 = CheckStraght (value,3,true);

		if (newList != null && newList2 != null && newList3 != null) {
			if (CheckColor (newList) && CheckColor (newList2) && CheckColor (newList3)) {
				List<List<int>> temp = new List<List<int>> ();
				temp.Add (newList);
				temp.Add (newList2);
				temp.Add (newList3);
				return temp;
			} 
		} 

		list = backUp;
		return null;
	}

	//检查是否可以三同花
	private List<List<int>> TryThreeSameColor(List<int> value){

		List<int> backUp = new List<int>(value.ToArray());
		List<int> newList = GetSameColor (value,5,true);
		List<int> newList2 = GetSameColor (value,5,true);
		List<int> newList3 = GetSameColor (value,3,true);

		if (newList != null && newList2 != null && newList3 != null) {

			List<List<int>> temp = new List<List<int>> ();

			temp.Add (newList);
			temp.Add (newList2);
			temp.Add (newList3);

			return temp;

		} 

		list = backUp;
		return null;
	}

	//尝试组一个顺子
	private List<int> CheckStraght( List<int> value,int length = 5,bool dele = false){
		value.Sort ();
		value.Reverse ();
		List<int> Straght = new List<int> ();

		for(int i = 0 ; i < value.Count ; i++ ){

			Straght = new List<int> ();

			if ( value [i] > 9 && value [i] < 14 ) {
				continue;
			}

			if ( value [i] > 22 && value [i] < 27 ) {
				continue;
			}

			if ( value [i] > 35 && value [i] < 40 ) {
				continue;
			}

			if ( value [i] > 48 ) {
				continue;
			}

			Straght.Add (value [i]);

			for( int f = 1 ; f < value.Count ; f++ ){

				if ( Straght.Count >= 5 ) {
					break;
				}

				if (value.Contains (value [i] + f)) {
					Straght.Add (value [i] + f);
					continue;
				}

				if (value.Contains (value[i] + MaxCard + f)  ) {
					Straght.Add (value[i] + MaxCard + f);
					continue;
				}

				if (value.Contains (value[i] + 2 * MaxCard + f) ) {
					Straght.Add (value[i] + 2 * MaxCard + f);
					continue;
				}

				if (value.Contains (value[i] + 3 *  MaxCard + f)) {
					Straght.Add (value[i] + 3 * MaxCard + f);
					continue;
				}
				break;
			}

			if(Straght.Count == length){
				if ( dele ) {
					for( int t = 0 ; t < Straght.Count ; t++ ){
						value.Remove (Straght[t]);
					}
				}
				return Straght;
			}
		}

		return null;
	}
	/*
		1 - 13 是红桃2-A 
		14 - 26 是黑桃 2-A
		27- 39 是方片2-A
		40 - 52 是 梅花 2-A
		*/
	/*
	private List<int> CheckStraght( List<int> value,int length = 5,bool dele = false){

		value.Sort ();
		value.Reverse ();

		List<int> Straght = new List<int> ();

		for(int i = 0 ; i < value.Count ; i++ ){

			Straght = new List<int> ();

			if ( value [i] > 14 && value [i] < 18 ) {
				continue;
			}

			if ( value [i] > 27 && value [i] < 31 ) {
				continue;
			}

			if ( value [i] > 40 && value [i] < 44 ) {
				continue;
			}

			if ( value [i] < 5 ) {
				continue;
			}

			Straght.Add (value [i]);

			for( int f = 1 ; f < value.Count ; f++ ){

				if ( Straght.Count >= 5 ) {
					break;
				}

				if (value.Contains (value [i] - f)) {
					Straght.Add (value [i] - f);
					continue;
				}

				if (value.Contains (value[i] - MaxCard - f)  ) {
					Straght.Add (value[i] - MaxCard + f);
					continue;
				}

				if (value.Contains (value[i] - 2 * MaxCard - f) ) {
					Straght.Add (value[i] - 2 * MaxCard - f);
					continue;
				}

				if (value.Contains (value[i] - 3 *  MaxCard - f)) {
					Straght.Add (value[i] - 3 * MaxCard - f);
					continue;
				}
				break;
			}

			if(Straght.Count == length){
				if ( dele ) {
					for( int t = 0 ; t < Straght.Count ; t++ ){
						value.Remove (Straght[t]);
					}
				}
				return Straght;
			}
		}

		return null;
	}
	*/
	//检查是否同花
	private bool CheckColor( List<int> value){

		value.Sort ();

		int now = value [0];

		bool flag = true;

		if( value[0] > 0 && value[0] < 14 ){
			for(int i = 1 ; i < value.Count ; i++ ){
				if (value [i] > 0 && value [i] < 14) {

				} else {
					return false;
				}
			}
		}

		if( value[0] >= 14 && value[0] < 27 ){
			for(int i = 1 ; i < value.Count ; i++ ){
				if (value [i] >= 14 && value [i] < 27) {

				} else {
					return false;
				}
			}
		}

		if( value[0] >= 27 && value[0] < 40 ){
			for(int i = 1 ; i < value.Count ; i++ ){
				if (value [i] >= 27 && value [i] < 40) {

				} else {
					return false;
				}
			}
		}


		if(value[0] >= 40 && value[0] < 53){
			for(int i = 1 ; i < value.Count ; i++ ){
				if (value [i] >= 40 && value [i] < 53) {

				} else {
					return false;
				}
			}
		}

		return flag;
	}

	private string ChangName(int value){
		/*
		1 - 13 是红桃2-A 
		14 - 26 是黑桃 2-A
		27- 39 是方片2-A
		40 - 52 是 梅花 2-A
		*/

		string values = "";
		if( value > 0 && value < 14 ){
			values = "红桃";
			if(value < 10){
				values += value + 1;
			}

			if(value == 10){
				values += "J";
			}

			if(value == 11){
				values += "Q";
			}

			if(value == 12){
				values += "K";
			}

			if(value == 13){
				values += "A";
			}
		}

		if( value >= 14 && value < 27 ){
			values = "黑桃";
			if(value < 23){
				values += (value-12);
			}

			if(value == 23){
				values += "J";
			}

			if(value == 24){
				values += "Q";
			}

			if(value == 25){
				values += "K";
			}

			if(value == 26){
				values += "A";
			}
		}

		if( value >= 27 && value < 40 ){
			values = "方片";
			if( value < 36 ){
				values += (value-25);
			}

			if(value == 36){
				values += "J";
			}

			if(value == 37){
				values += "Q";
			}

			if(value == 38){
				values += "K";
			}

			if(value ==39){
				values += "A";
			}
		}


		if(value >= 40 && value < 53){
			values = "梅花";
			if(value < 49){
				values += (value-38);
			}

			if(value == 49){
				values += "J";
			}

			if(value == 50){
				values += "Q";
			}

			if(value == 51){
				values += "K";
			}

			if(value ==52){
				values += "A";
			}
		}
		return values;
	}





	//获取一个同花
	private List<int> GetSameColor( List<int> value ,int length=5,bool dele = false){

		value.Sort ();

		List<int> list_1 = new List<int> ();
		List<int> list_2 = new List<int> ();
		List<int> list_3 = new List<int> ();
		List<int> list_4 = new List<int> ();

		for( int i = 0 ; i < value.Count ; i++ ){
			if( value[i] > 0 && value[i] < 14 ){
				list_1.Add (value[i]);
			}

			if( value[i] >= 14 && value[i] < 27 ){
				list_2.Add (value[i]);
			}

			if( value[i] >= 27 && value[i] < 40 ){
				list_3.Add (value[i]);
			}


			if( value[i] >= 40 && value[i] < 53){
				list_4.Add (value[i]);
			}

			if( list_1.Count == length ){
				DeleteListItem (value, list_1);
				return list_1;
			}

			if( list_2.Count == length ){
				DeleteListItem (value, list_2);
				return list_2;
			}

			if( list_3.Count == length ){
				DeleteListItem (value, list_3);
				return list_3;
			}

			if( list_4.Count == length ){
				DeleteListItem (value, list_4);
				return list_4;
			}
		}

		return null;
	}

	private void DeleteListItem(List<int> value,List<int> target){
		for( int i = 0 ; i < target.Count ; i++ ){
			value.Remove (target [i]);
		}
	}

	//获取一个铁支 或者三条 或者 对子
	private List<int> GetIronOrThreeOrDouble( List<int> value,int length = 4,bool dele = false){
		value.Sort ();
		List<int> temp = new List<int> ();

		for( int i = 0 ; i < value.Count ; i++ ){

			temp = new List<int> ();
			temp.Add (value [i]);

			for(int f = 1 ; f < 4 ; f++){

				if(value.Contains(value[i] + MaxCard * f)){
					temp.Add (value [i] + MaxCard * f);
				}

				if( temp.Count == length ){
					if(dele){
						DeleteListItem (value, temp);
					}
					return temp;
				}
			}
		}

		return null;
	}

	//获取一个葫芦
	private List<int> GetGourd( List<int> value){

		List<int> three = GetIronOrThreeOrDouble (value, 3);
		List<int> Double =  GetIronOrThreeOrDouble (value, 2);
		if(three != null && Double != null){
			List<int> result = new List<int> ();
			for(int i = 0 ; i < three.Count ; i++ ){
				result.Add (three [i]);
			}

			for(int i = 0 ; i < Double.Count ; i++ ){
				result.Add (Double [i]);
			}
			return result;
		}
		return null;
	}

	public Combination GetStraghtColor(List<int> l){

		List<int> Straight = CheckStraght (l);
		if( Straight != null ){

			if (CheckColor (Straight)) {

				Combination combins = new Combination ();
				combins.Type = Combination.StraightColor;
				combins.setValue (Straight);
				return combins;
			}
		}

		return null;
	}

	public Combination GetBranch( List<int> l ){

		List<int> Branch = GetIronOrThreeOrDouble (l, 4, true);

		if ( Branch != null ) {

			Combination combins = new Combination ();
			combins.Type = Combination.Branch;
			combins.setValue (Branch);
			return combins;

		}

		return null;
	}

	public Combination GetGourds( List<int> l ){

		List<int> Gourdhead = GetIronOrThreeOrDouble (l,3,true);

		if (Gourdhead != null) {

			List<int> Gourdsfix = GetIronOrThreeOrDouble (l, 2, true);

			if (Gourdsfix != null) {

				Combination combins = new Combination ();
				combins.Type = Combination.Gourd;
				combins.setValue (Gourdhead);
				combins.getValue ().AddRange (Gourdsfix.ToArray ());
				return combins;

			} else {
				l.AddRange (Gourdhead.ToArray ());
			}
		}

		return null;
	}

	public Combination GetSameColors( List<int> l ){
		List<int> SameColor = GetSameColor (l,5,true);
		if ( SameColor != null ) {
			Combination combins = new Combination ();
			combins.Type = Combination.SameColor;
			combins.setValue (SameColor);
			return combins;
		}
		return null;
	}

	public Combination GetStraight( List<int> l ){

		List<int> Straght = CheckStraght (l,5,true);

		if ( Straght != null ) {
			Combination combins = new Combination ();
			combins.Type = Combination.Straight;
			combins.setValue (Straght);
			return combins;
		}

		return null;
	}

	public Combination GetThree( List<int> l ){

		List<int> Three = GetIronOrThreeOrDouble (l, 3, true);

		if (Three != null) {

			Combination combins = new Combination ();
			combins.Type = Combination.Three;
			combins.setValue (Three);
			return combins;
		}

		return null;
	}

	public Combination GetThrees( List<int> l ){

		List<int> Three = GetIronOrThreeOrDouble (l, 3, true);

		if (Three != null) {

			Combination combins = new Combination ();
			combins.Type = Combination.Three;
			combins.setValue (Three);
			return combins;
		}

		return null;
	}

	public Combination GetDoubleSub( List<int> l ){

		List<int> headSub = GetIronOrThreeOrDouble (l, 2, true);

		if (headSub != null) {

			List<int> backSub = GetIronOrThreeOrDouble (l, 2, true);

			if (backSub != null) {

				Combination combins = new Combination ();
				combins.Type = Combination.DoubleSub;
				headSub.AddRange (backSub.ToArray ());
				combins.setValue (headSub);
				return combins;

			} else {
				l.AddRange (headSub.ToArray ());
			}
		}
		return null;
	}

	public Combination GetSub( List<int> l ){

		List<int> headSub = GetIronOrThreeOrDouble (l, 2, true);

		if (headSub != null) {

			Combination combins = new Combination ();
			combins.Type = Combination.Sub;
			combins.setValue (headSub);
			return combins;
		}
		return null;
	}

	void Update(){

	}

}